#!/usr/bin/python3
# coding=utf-8
import json
import sys

###############################################################################
##  如前端入参是这个：
##  {
##    "instance":"192.168.1.101",
##    "timeout":"30"
##  }
###############################################################################
class Param(dict):
    def __missing__(self,key):
        sys.stderr.write("入参检查失败，没有检查到入参'%s'"%key)
        exit(1)

args = Param(json.loads(sys.argv[1]))

result = {}
result['commands'] = []

cmd0 = {}
cmd0['instance'] = args["instance"]
cmd0_arg_T = args.get("timeout","30")
cmd0_arg_yaml = '/etc/sysak/base.yaml'
cmd0_arg_log = '/var/log/sysak/iosdiag/iodiagnose/iodiagnose.log'
if int(cmd0_arg_T) <= 0:
	cmd0_arg_T = 30

dump_log_cmd = "cat /var/log/sysak/iosdiag/iodiagnose/iodiagnose.log 2>/dev/null;"
iosdiag_cmd = "sysak ioMonitor -y "+cmd0_arg_yaml+" -t "+str(cmd0_arg_T)+" -a "+cmd0_arg_log+" > /dev/null"
print_result_cmd = (
    "if [ ! -e {logfile} ]; then "
    "echo \"normal\"; "
    "else "
    "{dumpcmd} "
    "fi"
).format(logfile=cmd0_arg_log, dumpcmd=dump_log_cmd)
cmd0['cmd'] = iosdiag_cmd+" && "+print_result_cmd

result['commands'].append(cmd0)

data = json.dumps(result)
print(data)
